#!/usr/bin/python

import os, pickle

def initPickleDir(pickleDir, scriptPickleDir):
	# pickleDir should be something like .pickles
	# scriptPickleDr should be the python script name without the .py extension
	# returns absolute path to pickle directory
	# this function creates the directory for pickles, and subdirectory
	# for this scripts pickles
	
	# if the pickle directory doesn't exist
	# make both
	if(not os.path.isdir(pickleDir)): 
		os.mkdir(pickleDir)
		os.mkdir(os.path.join(pickleDir, scriptPickleDir))
	#if the .pickle directory exists, make sure one for this script does
	elif(not os.path.isdir(os.path.join(pickleDir, scriptPickleDir))):
		os.mkdir(os.path.join(pickleDir, scriptPickleDir))
	else:
		#we have all the directories we need
		pass
	picklePath=os.path.join(os.getcwd(),os.path.join(pickleDir, scriptPickleDir))
	return picklePath
	
def addPickle(obj, filename, pickleDir):
	# filenames should correspond to the name of variables
	# or the programmer should make note of what he names them
	# objects that have state information like sockets or threads
	# probably shouldn't be pickled
	filepath=os.path.join(pickleDir, filename)
	fh=open(filepath, 'wb')
	pickle.dump(obj, fh)
	fh.close()
	return filepath

def getPickle(filename, pickleDir):
	#returns the object from the pickle
	filepath=os.path.join(pickleDir, filename)
	fh=open(filepath, 'rb')
	obj=pickle.load(fh)
	return obj

def pickleExists(filename, pickleDir):
	#determines returns if a file exists
	return os.path.isfile(os.path.join(pickleDir, filename))
	

